# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/07/16 16:54
# @File           : views.py
# @IDE            : PyCharm
# @desc           : 主要接口文件

from fastapi import APIRouter, Depends
from motor.motor_asyncio import AsyncIOMotorDatabase
from utils.response import SuccessResponse
from . import crud, schemas
from apps.base.auth.validate import AllUserAuth, Auth
from .params import OperationParams
from core.database import mongo_getter

app = APIRouter()


###########################################################
#    日志管理
###########################################################
@app.get("/operations", summary="获取操作日志列表")
async def get_record_operation(
        p: OperationParams = Depends(),
        db: AsyncIOMotorDatabase = Depends(mongo_getter),
        auth: Auth = Depends(AllUserAuth())
):
    count = await crud.OperationRecordDal(db).get_count(**p.to_count())
    datas = await crud.OperationRecordDal(db).get_datas(**p.dict())
    return SuccessResponse(datas, count=count)


###########################################################
#    与外部系统接口监控
###########################################################
@app.get("/calls", summary="获取接口交互记录列表")
async def get_record_call(
        p: OperationParams = Depends(),
        db: AsyncIOMotorDatabase = Depends(mongo_getter),
        auth: Auth = Depends(AllUserAuth())
):
    count = await crud.ApiCallRecordDal(db).get_count(**p.to_count())
    datas = await crud.ApiCallRecordDal(db).get_datas(**p.dict(), v_return_objs=True)
    return SuccessResponse(datas, count=count)
